package com.food.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.food.dao.in.ManagerDAO;
import com.food.model.Manager;
import com.food.model.Member;

public class ManagerDAOJdbc implements ManagerDAO {

	private DataSource ds = null;
	public ManagerDAOJdbc() {
		try {
			Context ctx = new InitialContext();
			this.ds = (DataSource) ctx.lookup("java:comp/env/jdbc/food");
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}
	
	private static final String INSERT = "insert into manager (mgname, mgidentity, mgemail, mgtel, mgaccount, mgpassword) values (?,?,?,?,?,?)";
	@Override
	public void insertManager(Manager bean) {
		Connection conn = null;
		PreparedStatement stmt = null;
		try {

//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			stmt = conn.prepareStatement(INSERT);

			stmt.setString(1, bean.getMgname());
			stmt.setString(2, bean.getMgidentity());
			stmt.setString(3, bean.getMgemail());
			stmt.setString(4, bean.getMgtel());
			stmt.setString(5, bean.getMgaccount());
			stmt.setBytes(6, bean.getMgpassword());


			int i = stmt.executeUpdate();
			if (i == 1) {
				this.findManager(bean.getMgid());
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

	}

	@Override
	public void updateManager(Manager bean) {
		

	}

	@Override
	public void deleteManager(int mgid) {
		// TODO Auto-generated method stub

	}
	private static final String SELECT_BY_ACCOUNT = "select mgid,mgname, mgidentity, mgemail, mgtel, mgaccount, mgpassword from manager where mgaccount = ?";

	public Manager findManagerByAccount(String mgaccount){
		Manager result = null;

		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rset = null;
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			stmt = conn.prepareStatement(SELECT_BY_ACCOUNT);
			stmt.setString(1, mgaccount);
			rset = stmt.executeQuery();
			if (rset.next()) {
				result = new Manager();
				result.setMgid(rset.getInt("mgid"));
				result.setMgname(rset.getString("mgname"));
				result.setMgidentity(rset.getString("mgidentity"));
				result.setMgemail(rset.getString("mgemail"));
				result.setMgtel(rset.getString("mgtel"));
				result.setMgaccount(rset.getString("mgaccount"));
				result.setMgpassword(rset.getBytes("mgpassword"));

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rset != null) {
				try {
					rset.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	
	private static final String SELECT_BY_ID = "select mgid,mgname, mgidentity, mgemail, mgtel, mgaccount, mgpassword from manager where mgid = ?";

	@Override
	public Manager findManager(int mgid) {
		Manager result = null;

		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rset = null;
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			stmt = conn.prepareStatement(SELECT_BY_ID);
			stmt.setInt(1, mgid);
			rset = stmt.executeQuery();
			if (rset.next()) {
				result = new Manager();
				result.setMgid(rset.getInt("mgid"));
				result.setMgname(rset.getString("mgname"));
				result.setMgidentity(rset.getString("mgidentity"));
				result.setMgemail(rset.getString("mgemail"));
				result.setMgtel(rset.getString("mgtel"));
				result.setMgaccount(rset.getString("mgaccount"));
				result.setMgpassword(rset.getBytes("mgpassword"));

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rset != null) {
				try {
					rset.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (stmt != null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return result;

	}

}
